<template>
  <div class="animated fadeIn">
    <div class="row">
      <div class="col-md-12">
        <b-card>
          <div slot="header">
            <strong>产品详细</strong>
          </div>
          <product-form :pro="datas" @submit="handleSubmit" v-if="datas.goodid" mode="edit"></product-form>
        </b-card>
      </div>
    </div>
  </div>
</template>
<script>
  import { formatDate } from '../../service/date'
  import { findGoodsByGoodNo, editGoods } from '../../service/api'
  import IconSelector from '@/components/IconSelector.vue'
  import UploadImage from '@/components/UploadImage.vue'
  import ProductForm from '@/components/ProductForm.vue'
  import { delay } from '@/utils/common-tools'
  export default {
    name: 'pro-detail-edit',
    components: {
      IconSelector,
      UploadImage,
      ProductForm
    },
    data () {
      return {
        isShow: false,
        tips: '信息不能为空！',
        datas: {},
        isShowTime: false
      }
    },
    methods: {
      backRfi () {
        this.$router.push({
          path: '/information/rfi'
        })
      },
      /* handle submit */
      async handleSubmit (data) {
        const flag = this.$route.query.flag
        if (flag !== '2') {
          this.$router.go(-1)
          return
        }
        try {
          await this.$confirm('是否修改该条数据', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          })
          this.addUpdateProduct(data)
          console.log(data)
        } catch (e) {
          console.log(e)
          this.$message.info('已取消')
          await delay(500)
          this.$router.go(-1)
        }
      },
      /* 提交修改 */
      async addUpdateProduct (payload) {
        // const df = this.$refs.df
        // const fd = new FormData()
        // fd.append('user', localStorage.getItem('username'))
        // fd.append('icon', this.datas.icon)
        // fd.append('id', this.datas.id)
        const { err, data } = await editGoods({ ...payload, user: localStorage.getItem('username') })
        if (err || data.code !== 200) {
          this.$message.error('修改失败')
          return false
        }
        this.$message.success('修改完成')
        await delay(500)
        this.$router.go(-1)
      },
      /* 上传图片 */
      handleUpload (e) {
        // @todo
      }
    },
    created () {
      const { flag, id } = this.$route.query
      this.isShow = flag === '3'
      findGoodsByGoodNo({ goodno: id }).then(({ data, err }) => {
        if (data.code === 200) {
          this.datas = data.result
          if (data.result.updatetime) {
            this.datas.updatetime = formatDate(
              new Date(data.result.updatetime.time),
              'yyyy-MM-dd hh:mm:ss'
            )
          }
          for (const i in this.datas) {
            if (this.datas[i] === 0) {
              this.datas[i] = ''
            }
          }
        }
      })
    }
  }
</script>
